package club.jdiy.utils.poi;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * 要插入Excel的图片对象
 *
 */
public class ImageData {
    private File file;//图片文件
    private int width;//在Excel中的宽
    private int height;//在Excel中的高
    private int rowHeight;//Excel行高

    public ImageData(File file, int width, int height, int rowHeight) throws IOException {
        this.file = file;
        this.width = width;
        this.height = height;
        this.rowHeight = rowHeight;

        //width和height长宽都为0则取图片实际大小；
        // 一边为0，这一边自动(等比缩放)；
        // 两都不为0则拉伸(目前没有使用两边不为0的等比缩放)
        if (height == 0 || width == 0) {
            BufferedImage bufferedImage = ImageIO.read(file);
            int ww = bufferedImage.getWidth();
            int hh = bufferedImage.getHeight();
            if (height == 0 && width == 0) {
                this.height = hh;
                this.width = ww;
            } else if (height == 0) {
                this.height = hh * width / ww;
            } else {
                this.width = ww * height / hh;
            }
        }
    }

    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public int getRowHeight() {
        return rowHeight;
    }

    public void setRowHeight(int rowHeight) {
        this.rowHeight = rowHeight;
    }
}
